const flatten = obj => {
    let result = {};
    const next = (key, value) => {
        // 原始值类型处理
        if (value === null || !/^(object|function)$/i.test(typeof value)) {
            if (key) result[key] = value;
            return;
        }
        // 数组
        if (Array.isArray(value)) {
            if (value.length === 0) result[key] = [];
            value.forEach((item, index) => {
                next(`${key}[${index}]`, item);
            });
            return;
        }
        // 对象
        let keys = Reflect.ownKeys(value);
        if (key && keys.length === 0) result[key] = {};
        keys.forEach(item => {
            next(key ? `${key}.${item}` : `${item}`, value[item]);
        });
    }
    next('', obj);
    return result;
};
const obj = {
    a: 1,
    b: [1, 2, { c: true }],
    c: { e: 2, f: 3 },
    g: null,
};
console.log(flatten(obj)); //输出：{a:1,'b[0]':1,'b[1]':2,'b[2].c':true,'c.e':2,'c.f':3,g:null}